import java.util.Arrays;

public class Main {
    public static int solution(int[] stones) {
        if (stones.length == 1) {
            return 0; // 如果只有一个石子，则不能移动
        }

        Arrays.sort(stones);
        int n = stones.length;

        // 计算最大移动次数
        int maxMoves = Math.max(stones[n - 1] - stones[1], stones[n - 2] - stones[0]) - (n - 2);

        return maxMoves;
    }

    public static void main(String[] args) {
        System.out.println(solution(new int[]{7, 4, 9})); // 输出: 2
        System.out.println(solution(new int[]{6, 5, 4, 3, 10})); // 输出: 3
        System.out.println(solution(new int[]{1, 2, 3, 4, 5})); // 输出: 0
    }
}
